package com.tanhua.management.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.management.service.UserPageInfoService;
import com.tanhua.management.vo.PageResult;
import com.tanhua.management.vo.UserBaseInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("manage/users")
@Slf4j
public class UserPageInfoController {

    @Autowired
    private UserPageInfoService userPageInfoService;

    /**
     * 用户数据翻页
     *
     * @param page
     * @param pagesize
     * @param id       //搜索条件
     * @param nickname //搜索条件
     * @param city     //搜索条件
     * @return
     */
    @GetMapping
    public ResponseEntity<Object> queryUserPageInfo(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                    @RequestParam(value = "pagesize", required = false, defaultValue = "10") Integer pagesize,
                                                    @RequestParam(value = "id", required = false) String id,
                                                    @RequestParam(value = "nickname", required = false) String nickname,
                                                    @RequestParam(value = "city", required = false) String city) {
        try {
            PageResult pageResult = userPageInfoService.queryUserPageInfo(page, pagesize, id, nickname, city);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            log.error("查询用户数据翻页失败~ ", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户基本资料
     *
     * @param userID
     * @return
     */
    @GetMapping("{userID}")
    public ResponseEntity<Object> queryUserBaseInfo(@PathVariable Integer userID) {
        try {
            UserBaseInfoVo userBaseInfoVo = userPageInfoService.queryUserBaseInfo(userID);
            if (ObjectUtil.isNotEmpty(userBaseInfoVo)) {
                return ResponseEntity.ok(userBaseInfoVo);
            }
        } catch (Exception e) {
            log.error("查询用户数据翻页失败~ ", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 冻结用户
     * @param map
     * @return
     */
    @PostMapping("freeze")
    public ResponseEntity<Object> freezeUser(@RequestBody Map<String,String> map) {
        try {
            Integer userId = Convert.toInt(map.get("userId"));
            Integer freezingTime = Convert.toInt(map.get("freezingTime"));
            Integer freezingRange = Convert.toInt(map.get("freezingRange"));
            String reasonsForFreezing = Convert.toStr(map.get("reasonsForFreezing"));
            String frozenRemarks = Convert.toStr(map.get("frozenRemarks"));

            String message = userPageInfoService.freezeUser(userId,freezingTime,freezingRange,reasonsForFreezing,frozenRemarks);
            if (ObjectUtil.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("冻结用户失败~ ", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户解冻操作
     * @param map
     * @return
     */
    @PostMapping("unfreeze")
    public ResponseEntity<Object> unfreezeUser(@RequestBody Map<String,String> map) {
        try {
            Integer userId = Convert.toInt(map.get("userId"));
            String reasonsForThawing = map.get("frozenRemarks");

            String message = userPageInfoService.unfreezeUser(userId,reasonsForThawing);
            if (ObjectUtil.isNotEmpty(message)) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            log.error("用户解冻失败~ ", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
